package com.ray.project.gpslocation;

import android.location.Location;


import com.google.android.maps.GeoPoint;

import java.text.DecimalFormat;


public class GpsLocationHelper {

    public static final double MULTIPLIER = 1e6;
    private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("###.##");

    // If a GPS location can't be determined then default to the co-ordinates for HONOLULU, Hawaii.
    public static final GeoPoint HONOLULU = new GeoPoint((int) (21.18 * GpsLocationHelper.MULTIPLIER),
        (int) (-157.49 * GpsLocationHelper.MULTIPLIER));
    public static final GeoPoint TOKYO = new GeoPoint((int) (35.68 * GpsLocationHelper.MULTIPLIER),
            (int) (139.69 * GpsLocationHelper.MULTIPLIER));
    public static final GeoPoint DUBLIN = new GeoPoint((int) (53.20 * GpsLocationHelper.MULTIPLIER),
            (int) (-15.35 * GpsLocationHelper.MULTIPLIER));
    public static final GeoPoint VANCOUVER = new GeoPoint((int) (49.15 * GpsLocationHelper.MULTIPLIER),
            (int) (-123.60 * GpsLocationHelper.MULTIPLIER));
    public static final GeoPoint ANCHORAGE = new GeoPoint((int) (61.13 * GpsLocationHelper.MULTIPLIER),
            (int) (-149.53 * GpsLocationHelper.MULTIPLIER));
    public static final GeoPoint REYKJAVIK = new GeoPoint((int) (64.08 * GpsLocationHelper.MULTIPLIER),
            (int) (-21.56 * GpsLocationHelper.MULTIPLIER));
    
    public static GeoPoint getPreferenceGeoPoint(String latitude, String longitude) { 	    	    
 	    return new GeoPoint((int) (Double.parseDouble(latitude) * GpsLocationHelper.MULTIPLIER),
 	           (int) (Double.parseDouble(longitude) * GpsLocationHelper.MULTIPLIER)); 	    
    }
    
    public static GeoPoint getGeoPoint(final Location loc) {
        int latitude = (int) (loc.getLatitude() * GpsLocationHelper.MULTIPLIER);
        int longitude = (int) (loc.getLongitude() * GpsLocationHelper.MULTIPLIER);
        return new GeoPoint(latitude, longitude);
    }
   
    public static GeoPoint getGeoPoint(final String geoRssPoint) {
        GeoPoint returnGeoPoint = null;
        String gPoint = geoRssPoint.trim();
        if (gPoint.indexOf(" ") != -1) {
            String latitude = gPoint.substring(0, gPoint.indexOf(" "));
            String longtitude = gPoint.substring(gPoint.indexOf(" "), gPoint.length());
            double latituded = Double.parseDouble(latitude);
            double longituded = Double.parseDouble(longtitude);
            int lat = (int) (latituded * GpsLocationHelper.MULTIPLIER);
            int lon = (int) (longituded * GpsLocationHelper.MULTIPLIER);
            returnGeoPoint = new GeoPoint(lat, lon);
        }
        return returnGeoPoint;
    }

    // This method converts a numeric value to a string.
    public static String parseGeoPoint(final double point, final boolean isLat) {
        String geoPointString = GpsLocationHelper.DECIMAL_FORMAT.format(point);
        if (geoPointString.indexOf("-") != -1) {
            geoPointString = geoPointString.substring(1, geoPointString.length());
        }
        if (isLat) {
            if (point < 0) {
                geoPointString += "S";
            } else {
                geoPointString += "N";
            }
        }       
        else {
            if (point < 0) {
                geoPointString += "W";
            } else {
                geoPointString += "E";
            }
        }
        return geoPointString;
    }
}
